/**
 * @author Chris Wilcox
 * @author Preston Parrett
 * 
 * @date 2008/03/17
 * 
 * The Evolution program was written for UCSB's CS165b class.
 * 
 * References: The AppletLauncher code is borrowed significantly from
 * David Eck's AppletLauncher in his Genetic Algorithms example at:
 * http://math.hws.edu/xJava/GA/index.html
 */

package evolution;


import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

@SuppressWarnings("serial")
public class AppletLauncher extends Applet implements ActionListener {

   private Button launchButton;
   @SuppressWarnings("unused")
   private boolean frameIsOpen;
   private UI frame;

   public void init() {
      setLayout(new BorderLayout());
      launchButton = new Button("Start the World");
      add(launchButton, BorderLayout.CENTER);
      launchButton.addActionListener(this);
   }
   
   synchronized void frameHasOpened() {
      frameIsOpen = true;
      launchButton.setEnabled(true);
      launchButton.setLabel("End the World");
   }
   
   synchronized void frameHasClosed() {
      frameIsOpen = false;
      launchButton.setLabel("Start the World");
      frame = null;
   }
   
   synchronized public void destroy() {
      if (frame != null) {
         frame.close();
         frame = null;
      }
   }
   
   synchronized public void actionPerformed(ActionEvent evt) {
      if (frame == null) {
         launchButton.setEnabled(false);
         try {
            frame = new UI(this);
         }
         catch (Exception e) {
            launchButton.setEnabled(true);
            launchButton.setLabel("Error! Try Again?");
         }
      }
      else
         frame.close();
   }
}
